// TrayIcon.cs created with MonoDevelop
// User: kev at 22:54 23/11/2007
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using Gtk;
using System.Collections.Generic;

namespace GtkTest
{    
            
    public class TrayIcon : StatusIcon
    {
        private Gdk.Pixbuf iconpic;
        private Menu mainMenu;
        private Dictionary<string, TrayMenuItem>menuItem;
        
        public Dictionary<string, TrayMenuItem> MenuItem 
        {
            get 
            {
                return menuItem;
            }
        }
        
        public void AddMenuItem(string szName, string szImagePath)
        {
            TrayMenuItem tmItem = new TrayMenuItem(szName);
            Gdk.Pixbuf icon = new Gdk.Pixbuf(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, szImagePath), 18, 18);
            Image img = new Image(icon);
            tmItem.Image = img;
            menuItem.Add(szName, tmItem);
            mainMenu.Add(menuItem[szName]);
        }
        
        public TrayIcon() : this("./resources/Chat.png")
        {
        }
        
        public TrayIcon(string szIconImage) : base()
        {
            iconpic = new Gdk.Pixbuf(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, szIconImage));
            this.Pixbuf = iconpic;
            this.PopupMenu += new PopupMenuHandler(onPopupMenu) ;
            
            mainMenu = new Menu();            
            menuItem = new Dictionary<string,TrayMenuItem>();            
        }

        public void UpdateTrayIcon(string szIconImage)
        {
            iconpic = new Gdk.Pixbuf(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, szIconImage));
            this.Pixbuf = iconpic;
        }
        
        
        public void onPopupMenu(object o, PopupMenuArgs args)
        {
            mainMenu.ShowAll();
            mainMenu.Popup();
            args.RetVal = true;
        }
        
        
        public class TrayMenuItem : ImageMenuItem
        {
			public delegate void SubMenuClickDelegate(string MenuName);
			public event SubMenuClickDelegate SubMenuActivated;
            private Menu subMenu;
            private Dictionary<string, ImageMenuItem>menuItem;
        
            public Dictionary<string, ImageMenuItem>MenuItem 
            {
                get 
                {
                    return menuItem;
                }
            }

            public TrayMenuItem() : this("Click here...")
            {
            }
            
            public TrayMenuItem(string label) : base(label)
            {
                subMenu  = new Menu();
                menuItem = new Dictionary<string,ImageMenuItem>();
            }

            public void AddSubMenu(string szName, string szImagePath)
            {
                ImageMenuItem mItem = new ImageMenuItem(szName);
                Gdk.Pixbuf icon = new Gdk.Pixbuf(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, szImagePath), 18, 18);
                Image img = new Image(icon);
                mItem.Image = img; 
                menuItem.Add(szName, mItem);
                subMenu.Add(menuItem[szName]);
				
				// Build up events for each sub menu item
				mItem.Activated += new System.EventHandler(OnSubMenuActivated);
                
                this.Submenu = subMenu;
            }
			
			public void OnSubMenuActivated(object sender, EventArgs e)
			{
				if(sender is Gtk.ImageMenuItem)
				{
					ImageMenuItem Item = (ImageMenuItem)sender;
					
					foreach( KeyValuePair<string, ImageMenuItem> item in menuItem)
					{
						if(item.Value == Item)
						{
							if(SubMenuActivated != null)
							{
								SubMenuActivated(item.Key);
							}
							break;
						}
					}					
				}
			}
        }
    }
}
